package notwork;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class udpEchoClient_1 {
    private DatagramSocket socket=null;
    private String serverIp;
    private int serverPort;

    public udpEchoClient_1(String serverIp, int serverPort) throws SocketException {
        this.serverIp = serverIp;
        this.serverPort = serverPort;
        socket=new DatagramSocket();
    }

    public void start() throws IOException {
        Scanner in=new Scanner(System.in);
        while(true){
            System.out.println("请输入信息：");
            if(!in.hasNext()){
                break;
            }
            String request=in.next();
            DatagramPacket requestPacket=new DatagramPacket(request.getBytes(),request.getBytes().length,
                    InetAddress.getByName(serverIp),serverPort);
            socket.send(requestPacket);

            DatagramPacket reponsePacket=new DatagramPacket(new byte[4096],4096);
            socket.receive(reponsePacket);
            String reponse=new String(reponsePacket.getData(),0,reponsePacket.getLength());
            System.out.println(reponse);
        }
    }

    public static void main(String[] args) throws IOException {
        udpEchoClient_1 udpEchoClient_1=new udpEchoClient_1("127.0.0.1",8989);
        udpEchoClient_1.start();
    }
}
